package org.dgmwebTest.hg.download;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class DownLoadExcelServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DownLoadExcelServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
		}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List head = new ArrayList<String>();
		head.add("头一");
		head.add("头二");
		head.add("头三");
		
		List allContentList = new ArrayList<String>();
		for (int i = 0; i < 1000; i++) {
			List content2 = new ArrayList<String>();
			content2.add("content..");
			content2.add("content..");
			content2.add("content..");
			allContentList.add(content2);
		}
		
		OutputStream os = null;
		
		String fileName = "你好.xls";
		byte[] b = fileName.getBytes("GBK");
		fileName = new String(b, "iso8859-1");

		try {
			response.setContentType("application/x-download");
			response.setHeader("Content-disposition", "attachment; filename=" + fileName);
			 response.setCharacterEncoding("utf8"); 
			 response.setContentType("text/html;charset=utf8");
			HSSFWorkbook workbook = (new PoiCustomComm()).getExcelWorkBook(head, allContentList);
			
			os = response.getOutputStream();
			workbook.write(os);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			os.close();
		}
//		try {
//			
//			byte[] b  = new byte[1024];
//			int i =0;
//			
//			while((i=fis.read(b))>0){
//				os.write(b);
//			}
//			os.flush();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			os.close();
//			fis.close();
//			e.printStackTrace();
//		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
